﻿/*
 * Created by SharpDevelop.
 * User: Intel
 * Date: 24.12.2014
 * Time: 14:48
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.Linq;
using System.Linq;
using Region.Utils;
namespace Invent
{
  /// <summary>
  /// Description of PrintForm410.
  /// </summary>
  public partial class PrintForm410 : Form
  {
    DatabaseInvent m_db;
    Form m_form;
    
    public PrintForm410(Form form,DatabaseInvent db)
    {
      m_db = db;
      m_form = form;
      //Devices = new Dictionary<Guid, DataRow>();
      //
      // The InitializeComponent() call is required for Windows Forms designer support.
      //
      InitializeComponent();
      //
      // TODO: Add constructor code after the InitializeComponent() call.
      //
      
    }
    public void AddDevice(Guid DeviceUID)
    {
      foreach(var i in listBox1.Items)
        if ((i as ListItemValue).Value.Equals(DeviceUID))
          return;
      foreach(var r in 
              (
              from row in m_db.Devices.AsEnumerable()
              where row.Field<Guid>("DeviceUID")==DeviceUID 
              select new {
                Name=(String)row["DeviceName"],
                UID=row["DeviceUID"],
                EmployeeUID = (Guid)row["EmployeeUID"]
              }))
      {
        String FIO = (
          from row in m_db.Employee.AsEnumerable() 
          where (Guid)row["EmployeeUID"]==r.EmployeeUID
          select (String)row["EmployeeName"]
         ).SingleOrDefault();
        listBox1.Items.Add(new ListItemValue(FIO+" "+r.Name,r.UID));
      }
    }
    void showList()
    {
       
    }
    public void setMove()
    {
      this.Left = m_form.Left+m_form.Width;
      this.Top = m_form.Top;
      this.Height = m_form.Height;
    }
    void PrintForm410Shown(object sender, EventArgs e)
    {
      setMove();
    }
    void Button2Click(object sender, EventArgs e)
    {
      listBox1.Items.Clear();
    }
    void Button1Click(object sender, EventArgs e)
    {
      var Table1 = new List<Dictionary<string, object>>();
      var Table2 = new List<Dictionary<string, object>>();
      int Num=0;
      foreach(var i in listBox1.Items)
      {
        Guid DeviceUID = (Guid)((ListItemValue)i).Value;
        foreach(var r in 
              (
              from row in m_db.Devices.AsEnumerable() 
              join row2 in m_db.Employee.AsEnumerable() on (Guid)row["EmployeeUID"] equals (Guid)row2["EmployeeUID"]
              where row.Field<Guid>("DeviceUID")==DeviceUID 
              select new {
                Name=(String)row["DeviceName"],
                UID=row["DeviceUID"],
                FIO=(String)row2["EmployeeName"]
              }))
        {
          var nr = new Dictionary<string, object>();
          nr["%Table1%"] = "";
          nr["%Table2%"] = "";
          nr["%Name%"] = r.Name;
          nr["%FIO%"] = r.FIO;
          if (Num++ >= 19)
            Table2.Add(nr);
          else
            Table1.Add(nr);
        }
        
      }
      var nrnull = new Dictionary<string, object>();
      nrnull["%Table1%"] = "";
      nrnull["%Table2%"] = "";
      nrnull["%Name%"] = "";
      nrnull["%FIO%"] = "";
      nrnull["один"] = "";
      
      for(Int32 k=Table1.Count;k<19;k++) Table1.Add(nrnull);
      for(Int32 k=Table2.Count;k<33;k++) Table2.Add(nrnull);
      m_db.ExportXLSX("410.xltx",new Dictionary<string, object>(),
                 new Dictionary<string, List<Dictionary<string, object>>>{{"%Table1%",Table1},{"%Table2%",Table2}});
    }
  }
}
